﻿using Sustainalytics.ESGRatings.Computing.Entities;
using Sustainalytics.ESGRatings.Computing.Entities.Governance;
using Sustainalytics.ESGRatings.DataContract;
using System;
using System.Collections.Generic;
using Sustainalytics.ESGComputing.Service.Tests.GovernanceUnitTests.Fixtures;

namespace Sustainalytics.ESGComputing.Service.Tests.GovernanceUnitTests
{
    public sealed class GovernanceComputingFixture : BaseGovernanceFixture
    {
        private readonly Guid _companyId = new Guid("00000000-0000-0000-0000-000000000003");
        private bool _disposed;

        public GovernanceComputingFixture()
        {
            DataSet = new GovernanceDataContainer
            {
                SetId = SetId,
                IndicatorTemplates = GetIndicatorTemplates(),
                Companies = GetCompanies(),
                CompaniesRawScores = GetCompaniesRawScores(),
                Modifiers = GetCombinationModifiers(),
                WeightMatrix = GetWeightsMatrix()
            };

            try
            {
                var taskAsync = SutClient.PostAsync(DataSet);

                var result = taskAsync.Result;
                result.EnsureSuccessStatusCode();
            }
            catch (Exception ex)
            {
            }
        }

        ~GovernanceComputingFixture()
        {
            Dispose(false);
        }

        public GovernanceDataContainer DataSet { get; private set; }

        public override Guid MatrixId
        {
            get
            {
                return
                    new Guid("00000000-0000-0000-0000-000000000002");
            }
        }

        public override Guid SetId
        {
            get { return new Guid("00000000-0000-0000-0000-000000000001"); }
        }

        protected override void Dispose(bool disposing)
        {
            if (_disposed)
                return;

            if (disposing)
            {
                try
                {
                    var taskClean = SutClient.DeleteAsync(SetId);
                    taskClean.Result.EnsureSuccessStatusCode();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                finally
                {
                    SutClient.Dispose();
                }

                SutClient.Dispose();


            }
            _disposed = true;
        }

        protected override ICollection<CombinationModifier> GetCombinationModifiers()
        {
            return new List<CombinationModifier>
            {
                new CombinationModifier("G.1.1","G.1.2",0.3f),
                new CombinationModifier("G.1.1","G.1.3",0.05f),
                new CombinationModifier("G.1.2","G.1.3",0.02f),
                new CombinationModifier("G.2.1","G.2.2",0.05f),
            };
        }

        protected override ICollection<CompanyData> GetCompanies()
        {
            return new List<CompanyData>
            {
                new CompanyData( _companyId,"Banks","EUR")
            };
        }

        protected override ICollection<CompanyScores> GetCompaniesRawScores()
        {
            return new List<CompanyScores>
            {
                new CompanyScores(_companyId,
                    new List<IndicatorDefaultScore>
                    {
                        new IndicatorDefaultScore("G.1.1", 60),
                        new IndicatorDefaultScore("G.1.2", 10),
                        new IndicatorDefaultScore("G.1.3", 40),
                        new IndicatorDefaultScore("G.2.1", 40),
                        new IndicatorDefaultScore("G.2.2", 100),
                    })
            };
        }

        protected override ICollection<IndicatorTemplate> GetIndicatorTemplates()
        {
            return new List<IndicatorTemplate>
            {
                new IndicatorTemplate("G.1.1",  "G","G.1"),
                new IndicatorTemplate("G.1.2",  "G","G.1"),
                new IndicatorTemplate("G.1.3",  "G","G.1"),
                new IndicatorTemplate("G.2.1",  "G","G.2"),
                new IndicatorTemplate("G.2.2",  "G","G.2")
            };
        }

        protected override RegionMatrix GetWeightsMatrix()
        {
            return new RegionMatrix
            {
                MatrixId = MatrixId,
                IssuesWeights = new List<IssuesWeights>
                {
                    new IssuesWeights("EUR", new List<IndicatorData<float>>
                    {
                        new IndicatorData<float>("G.1", 0.25f),
                        new IndicatorData<float>("G.2", 0.75f),
                    })
                },
                Matrix = new List<RegionWeights>
                {
                    new RegionWeights("EUR", new List<IndicatorData<float>>
                    {
                        new IndicatorData<float>("G.1.1", 0.1f),
                        new IndicatorData<float>("G.1.2", 0.2f),
                        new IndicatorData<float>("G.1.3", 0.15f),
                        new IndicatorData<float>("G.2.1", 0.25f),
                        new IndicatorData<float>("G.2.2", 0.3f)
                    })
                }
            };
        }
    }
}